<?php
class Lib_General extends Zend_Controller_Plugin_Abstract {
	public static function encryptPassword($str, $key) {
		$block = mcrypt_get_block_size ( 'des', 'ecb' );
		$pad = $block - (strlen ( $str ) % $block);
		$str .= str_repeat ( chr ( $pad ), $pad );
		
		return mcrypt_encrypt ( MCRYPT_DES, $key, $str, MCRYPT_MODE_ECB ).$key;
	}
	public static function decryptPassword($string) {
		$key=substr($string, strlen($string)-8);
		$str=substr($string, 0,strlen($string)-8);
		$str = mcrypt_decrypt ( MCRYPT_DES, $key, $str, MCRYPT_MODE_ECB );
		
		$block = mcrypt_get_block_size ( 'des', 'ecb' );
		$pad = ord ( $str [($len = strlen ( $str )) - 1] );
		return substr ( $str, 0, strlen ( $str ) - $pad );
	}
	public static function getKey() {
		return self::random_key ( 8 );
	}
	private static function random_key() {
		// start with a blank password
		$password = "";
		// define possible characters - any character in this string can be
		// picked for use in the password, so if you want to put vowels back in
		// or add special characters such as exclamation marks, this is where
		// you should do it
		$possible = "2346789bcdfghjkmnpqrtvwxyzBCDFGHJKLMNPQRTVWXYZ";
		
		// we refer to the length of $possible a few times, so let's grab it now
		$maxlength = strlen ( $possible );
		
		// check for length overflow and truncate if necessary
		if (8 > $maxlength) {
			$length = $maxlength;
		}
		
		// set up a counter for how many characters are in the password so far
		$i = 0;
		
		// add random characters to $password until $length is reached
		while ( $i < 8 ) {
			
			// pick a random character from the possible ones
			$char = substr ( $possible, mt_rand ( 0, $maxlength - 1 ), 1 );
			
			// have we already used this character in $password?
			if (! strstr ( $password, $char )) {
				// no, so it's OK to add it onto the end of whatever we've
				// already got...
				$password .= $char;
				// ... and increase the counter by one
				$i ++;
			}
		}
		
		// done!
		return $password;
	}
	public static function clean_url($text) {
		$text = strtolower ( $text );
		$code_entities_match = array (' ','--','&quot;','!','@','#','$','%','^','&','*','(',')','_','+','{','}','|',':','"','<','>','?','[',']','\\',';',"'",',','.','/','*','+','~','`','=' );
		$code_entities_replace = array ('-','-','','','','','','','','','','','','','','','','','','','','','','','','' );
		$text = str_replace ( $code_entities_match, $code_entities_replace, $text );
		$text = str_replace ( '--', '-', $text );
		return $text;
	}
	
	public static function text2url($text = '') {
		return self::clean_url ( self::unUnicode ( $text ) );
	}
	public static function unUnicode($str = '') {
		$marTViet = array ("Ã ","Ã¡","áº¡","áº£","Ã£","Ã¢","áº§","Ã¢Ì�","áº­","áº©","áº«","Äƒ","áº±","áº¯","áº·","áº³","áºµ","Ã¨","Ã©","áº¹","áº»","áº½","Ãª","á»�","áº¿","á»‡","á»ƒ","á»…","Ã¬","Ã­","á»‹","á»‰","Ä©","Ã²","Ã³","á»�","á»�","Ãµ","Ã´","á»“","á»‘","á»™","á»•","á»—","Æ¡","á»�","á»›","á»£","á»Ÿ","á»¡","Ã¹","Ãº","á»¥","á»§","Å©","Æ°","á»«","á»©","á»±","á»­","á»¯","á»³","Ã½","á»µ","á»·","á»¹","Ä‘","Ã€","Ã�","áº ","áº¢","Ãƒ","Ã‚","áº¦","áº¤","áº¬","áº¨","áºª","Ä‚","áº°","áº®","áº¶","áº²","áº´","Ãˆ","Ã‰","áº¸","áºº","áº¼","ÃŠ","á»€","áº¾","á»†","á»‚","á»„","ÃŒ","Ã�","á»Š","á»ˆ","Ä¨","Ã’","Ã“","á»Œ","á»Ž","Ã•","Ã”","á»’","á»�","á»˜","á»”","á»–","Æ ","á»œ","á»š","á»¢","á»ž","á» ","Ã™","Ãš","á»¤","á»¦","Å¨","Æ¯","á»ª","á»¨","á»°","á»¬","á»®","á»²","Ã�","á»´","á»¶","á»¸","Ä�" );
		$marKoDau = array ("a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","e","e","e","e","e","e","e","e","e","e","e","i","i","i","i","i","o","o","o","o","o","o","o","o","o","o","o","o","o","o","o","o","o","u","u","u","u","u","u","u","u","u","u","u","y","y","y","y","y","d","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","E","E","E","E","E","E","E","E","E","E","E","I","I","I","I","I","O","O","O","O","O","O","O","O","O","O","O","O","O","O","O","O","O","U","U","U","U","U","U","U","U","U","U","U","Y","Y","Y","Y","Y","D" );
		return str_replace ( $marTViet, $marKoDau, $str );
	}
}